package calendar;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class CalendarUtil{
	private List<Holiday> holidayList;
	private int[] weekArray;
	private int[] daysOfMonth;
	private List<Appointment> appointmentList;

	public CalendarUtil(){
		holidayList = new ArrayList<Holiday>();
		Holiday h = new Holiday(1,1, "New Year");
		holidayList.add(h);
		h = new Holiday(1,31, "Lunah New Year");
		holidayList.add(h);
		h = new Holiday(2,1, "Lunah New Year");
		holidayList.add(h);
		h = new Holiday(2,2, "Lunah New Year");
		holidayList.add(h);
		h = new Holiday(2,3, "Lunah New Year");
		holidayList.add(h);
		h = new Holiday(4,5, "Ching Ming Festival");
		holidayList.add(h);
		h = new Holiday(4,18, "Good Friday");
		holidayList.add(h);
		h = new Holiday(4,19, "Good Friday");
		holidayList.add(h);
		h = new Holiday(4,21, "Easter Monday");
		holidayList.add(h);
		h = new Holiday(5,1, "Labour Day");
		holidayList.add(h);
		h = new Holiday(5,6, "Buddha's Birthday");
		holidayList.add(h);
		h = new Holiday(6,2, "Tuen Ng Festival");
		holidayList.add(h);
		h = new Holiday(7,1, "HK SAR Establishment Day");
		holidayList.add(h);
		h = new Holiday(9,9, "Day following Mid-Autumn Festival");
		holidayList.add(h);
		h = new Holiday(10,1, "National Day");
		holidayList.add(h);
		h = new Holiday(10,2, "Chung Yeung Festival");
		holidayList.add(h);
		h = new Holiday(12,25, "Christmas Day");
		holidayList.add(h);
		h = new Holiday(12,26, "Christmas Day");
		holidayList.add(h);
		
		weekArray = new int[12];
		weekArray[0] = 3;
		weekArray[1] = 6;
		weekArray[2] = 6;
		weekArray[3] = 2;
		weekArray[4] = 4;
		weekArray[5] = 0;
		weekArray[6] = 2;
		weekArray[7] = 5;
		weekArray[8] = 1;
		weekArray[9] = 3;
		weekArray[10] = 6;
		weekArray[11] = 1;

		daysOfMonth = new int[12];
		for(int i = 0; i < 7; i++){
			if(i % 2 == 0){
				daysOfMonth[i] = 31;
			}else{
				daysOfMonth[i] = 30;
			}
		}
		daysOfMonth[1] = 28;
		for(int i = 7; i < 12; i++){
			if(i % 2 == 1){
				daysOfMonth[i] = 31;
			}else{
				daysOfMonth[i] = 30;
			}
		}

		appointmentList = new ArrayList<Appointment>();
	}

	public List<Holiday> getHolidays(int month){
		List<Holiday> list = new ArrayList<Holiday>();
		Iterator<Holiday> ite = holidayList.iterator();
		while(ite.hasNext()){
			Holiday h = ite.next();
			if(month == h.getMonth()){
				list.add(h);
			}
		}
		return list;
	}
	
	public int getWeek(int month){
		if(month > 12 || month < 1){
			return -1;
		}
		return weekArray[month - 1];
	}
	public int getDayOfMonth(int month){
		return daysOfMonth[month - 1];
	}
	
	public void addAppointment(int month, int day, String event){
		appointmentList.add(new Appointment(month, day, event));
	}
	
	public void addWeeklyAppointment(int startMonth, int startDay, 
			int endMonth, int endDay, int week, String event){
		int firstDayWeek = getWeek(startMonth);
		int startDayWeek = (startDay - 1 + firstDayWeek) % 7;
		if(week < startDayWeek){
			week += 7;
		}
		int appointStartDay = startDay + week - startDayWeek;
		while(true){
			int daysOfStartMonth = getDayOfMonth(startMonth);
			if(appointStartDay > daysOfStartMonth){
				appointStartDay -= daysOfStartMonth;
				startMonth++;
			}
			if(startMonth < endMonth || (startMonth == endMonth && appointStartDay < endDay)){
				appointmentList.add(new Appointment(startMonth, appointStartDay, event));
				appointStartDay += 7;
			}else{
				break;
			}
		}
	}
	
	public void deleteAppoint(int month, int day){
		for(int i = 0; i < appointmentList.size(); i++){
			Appointment a = appointmentList.get(i);
			if(a.getMonth() == month && a.getDay() == day){
				appointmentList.remove(a);
			}
		}
	}
	
	public String dayType(int month, int day){
		String str = "";
		Iterator<Holiday> ite = holidayList.iterator();
		while(ite.hasNext()){
			Holiday h = ite.next();
			if(h.getMonth() == month && h.getDay() == day){
				str += h.getSymbol();
			}
		}
		Iterator<Appointment> iteA = appointmentList.iterator();
		while(iteA.hasNext()){
			Appointment a = iteA.next();
			if(a.getMonth() == month && a.getDay() == day){
				str += a.getSymbol();
			}
		}
		return str;
	}
	
	public String getAppointmentByDate(int month, int day){
		String returnValue = "";
		Iterator<Appointment> ite = appointmentList.iterator();
		while(ite.hasNext()){
			Appointment a = ite.next();
			if(a.getMonth() == month && a.getDay() == day){
				returnValue += a.getSymbol() + " " + a.getEvent() + "\n";
			}
		}
		return returnValue;
	}
	
	public String getAppointmentByMonth(int month){
		String returnValue = "";
		Iterator<Appointment> ite = appointmentList.iterator();
		while(ite.hasNext()){
			Appointment a = ite.next();
			if(a.getMonth() == month){ 
				returnValue += a.getSymbol() + " " + a.getDay() + " " + a.getEvent() + "\n";
			}
		}
		return returnValue;
	}
	
	public String getHolidaysByMonth(int month){
		String returnValue = "";
		Iterator<Holiday> ite = holidayList.iterator();
		while(ite.hasNext()){
			Holiday h = ite.next();
			if(h.getMonth() == month){ 
				returnValue += h.getSymbol() + " " + h.getDay() + " " + h.getEvent() + "\n";
			}
		}
		return returnValue;
	}
}
